var app = getApp(),
  $ = app.requirejs('jquery'),
  core = app.requirejs('core'),
  mainui = app.requirejs('mainui');
var api = require("../api.js")
module.exports = {
  get: function ($this, type, params, callback) {
    var self = this;
    // 测试自定义页面
    let apiurl = ''
    if (type == 'home') {
      apiurl = api.goods.getDiy()
    } else if (type == 'goodsdetail') {
      apiurl = api.goods.getGoodDiy()
    } else if (type == 'member') {
      apiurl = api.goods.getUser()
    } else {
      apiurl = api.goods.getDiypageDetail()
    }
    core.get(apiurl, {
      ...params
    }, function (result) {
      result.diypage = result.data
      for (var i in result.diypage.items) {
        if (result.diypage.items[i].id == 'topmenu') {
          $this.setData({
            topmenu: result.diypage.items[i]
          })
        }
      }
      var data = {
        loading: false,
        pages: result.data.data,
        usediypage: true,
        // startadv: result.startadv
      }

      if (result.data.hasOwnProperty('title')) {
        $this.setData({
          diytitle: result.data.title,
        });
      }

      let items = result.data.data ? JSON.parse(result.data.data) : []


      if (items != undefined) {
        
        let templates = type == 'home' || type == 'diy' || type == 'member' ? items.currentTemplate : items
        
        var arr = [];
        $.each(templates, function (id, item) {

          arr.push(item.id);
          if (item.components == 'mofang') {

            wx.getSystemInfo({
              success: function (res) {

                let ScreenWidth = res.screenWidth
                let ratio = ScreenWidth / 304
                item.options.combineLists.map((v, i) => {
                  v.width = (v.width - 1) * ratio
                  v.height = v.height * ratio
                  v.top = v.top * ratio
                  v.left = v.left * ratio
                })
              },
            })
          }
        });

        if (callback) {
          callback(result);
        }
        data.diypages = items;
        $this.setData(data);
      }
      if (callback) {
        callback(result);
      }
    });
  }

}